package com.hxzy.controller.system;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hxzy.common.annotation.Log;
import com.hxzy.common.core.controller.BaseController;
import com.hxzy.common.core.domain.AjaxResult;
import com.hxzy.common.core.domain.R;
import com.hxzy.common.core.domain.entity.PzServiceItem;
import com.hxzy.common.core.domain.entity.SysDictData;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.enums.BusinessType;
import com.hxzy.common.validator.AddValidator;
import com.hxzy.common.validator.EditValidator;
import com.hxzy.system.domain.dto.PzServiceItemDTO;
import com.hxzy.system.service.PzServiceItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;


@Api(tags = "中介服务查询")
@RequestMapping(value = "/api/tzxm/pzserviceitem")
@RestController
public class PzServiceItemController extends BaseController {

    @Autowired
   private PzServiceItemService pzServiceItemService;


    @ApiOperation(value = "分页获取中介用户信息")
    @Log(title = "中介服务", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/list")
    public R search(PzServiceItemDTO pzServiceItemDTO){
        IPage<PzServiceItem> page =this.pzServiceItemService.search(pzServiceItemDTO);
        return pageVO(page);
    }


    /**
     * 根据id查看详细信息
     * @param id
     * @return
     */
    @ApiOperation("根据id获取详细信息")
    @GetMapping(value = "/{id}")
    public R getInfo (@PathVariable Long id){
        PzServiceItem pzServiceItem = this.pzServiceItemService.getInfo(id);
        return R.okHasData(pzServiceItem);
    }

    /**
     * 中介服务 新增数据
     * @param pzServiceItem
     * @return
     */
    @ApiOperation("新增数据")
    @PostMapping
    public R addApi(@RequestBody @Validated(value = AddValidator.class) PzServiceItem pzServiceItem){
//        if (this.pzServiceItemService.selectByApproveItemCodeUnique(pzServiceItem.getApproveItemCode()) > 0){
//            return R.build(AckCode.EXISTS_VALUE);
//        }
        int count = this.pzServiceItemService.add(pzServiceItem);
        return count >0 ?  R.ok() : R.build(AckCode.NOT_INSERT);
    }

    /**
     * 行政审批事项编码是否唯一
     * @param approveItemCode
     * @return
     */
    @ApiOperation("行政审批事项编码是否唯一")
    @GetMapping(value = "/approveItemCode")
    public R approveItemCode( String approveItemCode){
        R r = R.ok();
        int count = this.pzServiceItemService.selectByApproveItemCodeUnique(approveItemCode);
        if (count > 0){
            r.setData(AckCode.EXISTS_VALUE);
        }
        return r;
    }

    @ApiOperation("修改数据")
    @PutMapping
    public R updateApi(@RequestBody @Validated(value = EditValidator.class ) PzServiceItem pzServiceItem){
//        if (this.pzServiceItemService.selectByApproveItemCodeUnique(pzServiceItem.getApproveItemCode()) > 0){
//            return R.build(AckCode.EXISTS_VALUE);
//        }
        int result = this.pzServiceItemService.updateByPzId(pzServiceItem);
        return result > 0 ? R.ok() : R.build(AckCode.NOT_UPDATE);
    }

    @ApiOperation("删除数据")
    @DeleteMapping(value = "/{ids}")
    public R removeApi(@PathVariable  Long[] ids){
//        如果传过来的id是空的话
        if (Objects.isNull(ids)){
            R.build(AckCode.SYSTEM_PARAM_FAIL);
        }
        boolean  result = this.pzServiceItemService.deleteByIds(ids);
        return result ? R.ok() : R.build(AckCode.NOT_DELETE);
    }

    /**
     * 行政审批事项列表
     * @return
     */
    @ApiOperation("行政审批事项下拉列表")
    @GetMapping(value = "/approveItemNameList")
    public R approveItemNameList(){
        List<SysDictData> list = this.pzServiceItemService.approveItemNameList();
        return R.okHasData(list);
    }

    /**
     * 中介资质类型列表
     */
    @ApiOperation("中介资质类型")
    @GetMapping(value = "/serviceTypeIdList")
    public R serviceTypeIdList(){
        List<SysDictData> list = this.pzServiceItemService.serviceTypeIdList();
        return R.okHasData(list);
    }

    /**
     * 委托方列表
     * @return
     */
    @ApiOperation("委托方")
    @GetMapping(value = "/consigorList")
    public R consigorList(){
        List<SysDictData> list = this.pzServiceItemService.consigorList();
        return R.okHasData(list);
    }

    /**
     * 资质等级列表
     * @return
     */
    @ApiOperation("资质等级")
    @GetMapping(value = "/certificateGradeList")
    public R certificateGradeList(){
        List<SysDictData> list = this.pzServiceItemService.certificateGradeList();
        return R.okHasData(list);
    }

    /**
     * 委托类型列表
     * @return
     */
    @ApiOperation("委托类型")
    @GetMapping(value = "/delegateTypeIdList")
    public R delegateTypeIdList(){
        List<SysDictData> list = this.pzServiceItemService.delegateTypeIdList();
        return R.okHasData(list);
    }

    /**
     * 是否收费列表
     * @return
     */
    @ApiOperation("是否收费")
    @GetMapping(value = "/serviceChargeList")
    public R serviceChargeList(){
        List<SysDictData> list = this.pzServiceItemService.serviceChargeList();
        return R.okHasData(list);
    }

    /**
     * 收费方列表
     * @return
     */
    @ApiOperation("收费方")
    @GetMapping(value = "/paymentList")
    public R paymentList() {
        List<SysDictData> list = this.pzServiceItemService.paymentList();
        return R.okHasData(list);
    }

}
